<?php

namespace app\controller;

use app\BaseController;
use think\captcha\facade\Captcha;
use think\facade\Db;

// 客户端(服务器)接口
class Client extends BaseController
{
    public function login()
    {
        $data = input('post.');
        $user = Db::table('tb_user')->where(['email' => $data['email'], 'password' => hash("sha256", "scnet_" . $data['password'])])->find();
        if ($user) {
            return (returnJson(msg: "登陆成功", data: $user));
        }
        return (returnJson(1, msg: "用户名或密码错误"));
    }
    public function getToken()
    {
        $data = input('post.');
        $user = Db::table('tb_user')->where(['Key'=>$data['Key'],'id'=>$data['id']])->find();
        if ($user) {
            $user['token'] = md5(uniqid());
            Db::table('tb_user')->where('id', $user['id'])->update(['token' => $user['token']]);
            return (returnJson(msg: "获取成功", data: ['token' => $user['token']]));
        }
        return (returnJson(1, msg: "获取失败"));
    }
    //验证token(获取用户信息)
    public function checkToken()
    {
        $data = input('post.');
        //如果有传入sid
        $sid = $data['sid'];
        $user = Db::table('tb_user')->where(['token' => $data['token'], 'id' => $data['id']])->find();
        if ($user) {
            //判断用户是否被封号
            if ($user['state'] != 1) {
                return (returnJson(1, msg: "用户已被禁用"));
            }
            if ($sid) {
                $server = Db::table('tb_server')->where('sid', $sid)->find();
                if ($server['blacklist'] == 1) {
                    $blacklist = Db::table('tb_server_blacklist')->where(['sid' => $sid, 'uid' => $user['id']])->find();
                    if ($blacklist)
                        return (returnJson(1, msg: "该用户被此服务器封禁"));
                }
                if ($server['whitelist'] == 1) {
                    $whitelist = Db::table('tb_server_whitelist')->where(['sid' => $sid, 'uid' => $user['id']])->find();
                    if (!$whitelist)
                        return (returnJson(1, msg: "该用户不在白名单内"));
                }
            }
            return (returnJson(msg: "验证成功", data: $user));
        }
        return (returnJson(1, msg: "验证失败"));
    }

    //记录加入服务器信息
    public function join_server()
    {
        $data = input('post.');
        if (strlen($data['sid']) != 32 || !preg_match('/^[0-9a-zA-Z]+$/', $data['sid'])) {
            return (returnJson(1, msg: "sid不合法"));
        }
        $user = Db::table('tb_user')->where(['token' => $data['token'], 'id' => $data['id']])->find();
        if (!$user) {
            return (returnJson(1, msg: "用户不存在"));
        }
        //插入记录
        Db::table('tb_record_play')->insert(['uid' => $user['id'], 'sid' => $data['sid'], 'ip' => $data['ip']]);
        return (returnJson(msg: "Success",data: ['uuid' => $user['id'], 'nickname' => $data['nickname'], 'rid' => ""]));
    }
    //记录离开服务器信息
    public function left_server()
    {

        $data = input('post.');
        //查询sid和uid 倒序第一条
        $result = Db::table('tb_record_play')->where(['uid' => $data['uid'], 'sid' => $data['sid']])->order('id desc')->find();
        if ($result['left_time']) {
            return (returnJson(1, msg: "不允许重复更新"));
        }
        Db::table('tb_record_play')->where('id', $result['id'])->update(['left_time' => date('Y-m-d H:i:s')]);
        return (returnJson(msg: "Success"));
    }

}